﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace JoinBoxAlignPlug
{
    public class Literal
    {
        public string ID { get; set; }
        public string ToolTip { get; set; }
        public string Default { get; set; }
    }

    public class SnippetXmlCodes
    {
        XmlNodeList _nodes;//总节点
        public SnippetXmlCodes(XmlDocument xmlDoc)
        {
            _nodes = xmlDoc.DocumentElement.ChildNodes;
        }

        public List<Literal> Declarations
        {
            get
            {
                var literals = new List<Literal>();
                string[] xmlArr = new string[] { "CodeSnippet", "Snippet", "Declarations" };
                GetXmlRecursion(_nodes, 0, xmlArr, declaration => {
                    if (declaration.Name != xmlArr[xmlArr.Length - 1])
                        return;
                    foreach (var dItem in declaration.ChildNodes)
                    {
                        var dXmlE = dItem as XmlElement;
                        if (dXmlE == null)
                            continue;
                        if (dXmlE.Name != "Literal")
                            continue;

                        var lt = new Literal();
                        foreach (var dNode in dXmlE.ChildNodes)
                        {
                            if (dNode is XmlElement literal)
                            {
                                switch (literal.Name)
                                {
                                    case "ID":
                                        lt.ID = literal.InnerText;
                                        break;
                                    case "Default":
                                        lt.Default = literal.InnerText;
                                        break;
                                    case "ToolTip":
                                        lt.ToolTip = literal.InnerText;
                                        break;
                                }
                            }
                        }
                        if (lt.ID != null)
                            literals.Add(lt);
                    }
                });
                return literals;
            }
        }

        public string Code
        {
            get
            {
                string codeStr = null;
                string[] xmlArr = new string[] { "CodeSnippet", "Snippet", "Code" };
                GetXmlRecursion(_nodes, 0, xmlArr, code => {
                    if (code.Name != xmlArr[xmlArr.Length - 1])
                        return;
                    foreach (var dItem in code.ChildNodes)
                    {
                        if (dItem is XmlCDataSection cd)
                        {
                            codeStr = cd.InnerText;
                        }
                    }
                });
                return codeStr;
            }
        }

        public string Header
        {
            get
            {
                string strTitle = null;
                string[] xmlArr = new string[] { "CodeSnippet", "Header" };
                GetXmlRecursion(_nodes, 0, xmlArr, code => {
                    if (code.Name != xmlArr[xmlArr.Length - 1])
                        return;
                    foreach (var dItem in code.ChildNodes)
                    {
                        if (dItem is XmlElement cd && cd.Name == "Title")
                        {
                            strTitle = cd.InnerText;
                            break;
                        }
                    }
                });
                return strTitle;
            }
        }

        /// <summary>
        /// 递归获取节点
        /// </summary>
        /// <param name="nodes">开始的节点</param>
        /// <param name="num">数组游标</param>
        /// <param name="xmlNodes">节点</param>
        /// <param name="action">委托</param>
        void GetXmlRecursion(XmlNodeList nodes, int num, string[] xmlNodes, Action<XmlElement> action)
        {
            foreach (var xa in nodes)
            {
                var xe = xa as XmlElement;
                if (xe == null)
                    continue;
                if (num < xmlNodes.Length && xe.Name == xmlNodes[num])
                {
                    GetXmlRecursion(xe.ChildNodes, ++num, xmlNodes, action);
                    action.Invoke(xe);
                }
            }
        }
    }
}


